<template>
  <div class="resource">
    <el-card shadow="hover">
      <div style="margin-bottom: 10px;">
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <el-button
          style="margin-left: 20px;"
          @click="search"
          icon="el-icon-search"
          type="primary"
          size="small"
          >搜索</el-button
        >
        <el-button
          @click="cancelData"
          icon="el-icon-circle-close"
          size="small"
          >重置</el-button
        >
      </div>
      <el-row :gutter="20">
        <el-form
          ref="params"
          :model="params"
          label-width="100px"
          size="small"
          class="searchForm"
        >
          <el-col :span="6">
            <el-form-item label="证书名称:" prop="name">
              <el-input v-model="params.name" size="small" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="选择分类:" prop="categoryId">
              <el-select
                v-model="params.categoryId"
                filterable
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in categoryIds"
                  :key="item.id"
                  :label="item.categoryName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建时间:" prop="certificate_createTime">
              <el-date-picker
                v-model="params.certificate_createTime"
                type="datetime"
                placeholder="选择日期时间"
                @change="createTime"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </el-card>

    <div class="container">
      <div class="handle-box">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
          class="mr10"
          >新增</el-button
        >
        <el-button
          type="danger"
          icon="el-icon-delete"
          class="handle-del mr10"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          >批量删除</el-button
        >
      </div>

      <!-- :height="tHeight" -->
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="证书名称" min-width="260" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属分类" min-width="260" align="center">
          <template slot-scope="scope">
            <span>{{ category(scope.row.categoryId) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="证书样片" align="center" min-width="100">
          <template slot-scope="scope">
            <img class="study_pic" :src="imgUrl + scope.row.pic" alt="" />
          </template>
        </el-table-column>
        <!-- <el-table-column
          label="证书介绍"
          min-width="260"
          prop="certificate_desc"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column> -->
        <el-table-column label="创建时间" align="center" min-width="160">
          <template slot-scope="scope">{{ formatT(scope.row.createTime) }}</template>
        </el-table-column>
        <el-table-column
          label="操作"
          min-width="200"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="params.pageNum"
          :limit.sync="params.pageSize"
          @pagination="getData00"
        />
      </div>
    </div>
    <!-- 新增编辑 -->
    <el-dialog :title="title666" :visible.sync="dialogVisible" width="600px">
      <el-form
        :rules="rules2"
        ref="formData"
        :model="formData"
        label-width="120px"
        size="small"
        class="form-box"
      >
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item label="证书样片" prop="pic">
              <el-upload
                class="avatar-uploader"
                :action="upImage + '/upload/file'"
                :show-file-list="false"
                :headers="headers"
                :on-success="handleAvatarSuccess"
              >
                <img
                  v-if="formData.pic"
                  :src="imgUrl + formData.pic"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="证书名称" prop="name">
              <el-input v-model="formData.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="证书类型" prop="categoryId">
              <treeselect
                :show-count="true"
                :disable-branch-nodes="true"
                v-model="formData.categoryId"
                :options="cateOptions"
                :normalizer="normalizer"
                placeholder="选择分类"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="20">
            <el-form-item label="证书介绍" prop="certificate_desc">
              <el-input
                type="textarea"
                :rows="6"
                v-model="formData.certificate_desc"
              ></el-input>
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取消</el-button>
        <el-button @click="submitForm()" type="primary" size="small"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  certificate__add,
  certificate__List,
  certificate__update,
  certificate__delete,
  certificateList,
} from "@/api/certificate/certificateAds";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect },
  props: [],
  data() {
    return {
      categoryIds: [],
      // 删除砖用
      multiple: true,
      ids: [],
      cateOptions: [],
      headers: { Authorization: "Bearer " + getToken() },
      upImage: process.env.VUE_APP_BASE_API,
      certificate_pic: "",
      cityList: [],
      teacher:
        "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2308044669,1028995539&fm=26&gp=0.jpg",
      title666: "",
      rules2: {
        categoryId: [
          { required: true, message: "请选择类型", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入老师名称", trigger: "blur" }],
        pic: [{ required: true, message: "请上传图片", trigger: "blur" }],
      },
      input: "",
      openFlag: true,
      loading: true,
      //数据
      tableData: [],
      params: {
        pageNum: 1,
        pageSize: 10,
        name:undefined,
        categoryId:undefined
      },
      pageTotal: 0,
      dialogVisible: false, // 是否展示编辑页面
      // 新增/编辑数据
      formData: {
        pic: "",
      },
      customer: {
        customerId: "",
      },
      paramsFlag: 0,
      editFlags: false,
    };
  },
  computed: {},
  created() {
    this.getData00();
    this.getTreeselect();
    this.getTreeselect2();
  },
  mounted() {},
  watch: {},
  methods: {
    // select(node,instanceId){
    //   if(node.id==0){
    //     this.$message.warning(`不可选择主类目,请重新选择`);
    //     this.formData.categoryId=undefined
    //   }
    // },
    // 用于表格分类帅选
    category(val) {
      var str = this.categoryIds.find(function (item) {
        return item.id == val;
      });
      if (str) return str.categoryName;
    },
    normalizer(node) {
      if (node.children == null || node.children == "null") {
        delete node.children;
      }
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.categoryName,
        children: node.children,
      };
    },
    // 列表下拉树结构
    getTreeselect() {
      certificateList(this.params).then((res) => {
        this.cateOptions = [];
        const menu = { id: 0, categoryName: "主类目", children: [] };
        menu.children = this.handleTree(res.rows, "id");
        this.cateOptions.push(menu);
      });
    },
    // 查询显示
    getTreeselect2() {
      certificateList({
        pageNum:1,
        pageSize:99999
      }).then((res) => {
        this.categoryIds=res.rows
      });
    },
    handleAvatarSuccess(res, file) {
      if (res.code == 200) {
        this.formData.pic = res.url;
        console.log(this.formData.pic);
        this.$message.success(`上传成功`);
      }
    },
    //   获取创建时间
    createTime(e) {
      console.log(e);
    },
    // 新增修改的方法
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          if (this.title666 == "新增") {
            certificate__add(this.formData).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message.success(`新增成功`);
                this.dialogVisible = false;
                this.getData00();
              }
            });
          } else {
            certificate__update(this.formData).then((res) => {
              if (res.code == 200) {
                this.$message.success(`修改成功`);
                this.dialogVisible = false;
                this.uploading = false;
                this.getData00();
              }
            });
          }
        }
      });
    },
    // 打开搜索
    openSearch() {
      this.openFlag = !this.openFlag;
    },
    // 重置搜索框
    cancelData() {
      this.resetForm("params");
      this.getData00();
    },
    handleAdd() {
      this.formData = {
        pic: "",
      };
      this.dialogVisible = true;
      this.title666 = "新增";
    },
    handleEdit(index, row) {
      this.formData = row;
      this.dialogVisible = true;
      this.title666 = "编辑";
    },
    // 删除操作
    handleSelectionChange(val) {
      this.ids = val.map((item) => item.id);
      this.single = val.length != 1;
      this.multiple = !val.length;
    },
    // 批量删除
    handleDelete(row) {
      console.log(row.id);
      const id = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return certificate__delete(id);
        })
        .then(() => {
          this.getData00();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    //获取数据
    getData00() {
      var _this = this;
      let data = this.params;
      certificate__List(data)
        .then((res) => {
          if (res.code == "200") {
            this.tableData = res.rows;
            this.pageTotal = res.total;
            this.loading = false;
          }
        })
        .catch(() => {
          console.log("error submit!!");
          return false;
        });
    },
    // 触发搜索按钮
    search() {
      this.$set(this.params, "pageNum", 1);
      this.paramsFlag = 0;
      this.getData00();
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.params, "pageNum", val);
      this.getData00();
    },
  },
};
</script>

<style scoped>
.resource {
  padding: 30px;
}
.container {
  margin-top: 20px;
}
.handle-box {
  margin-bottom: 10px;
}

.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.yellow {
  color: #f57506;
}
.green {
  color: green;
  cursor: pointer;
}
.blue {
  color: #409eff;
  cursor: pointer;
  text-decoration: underline;
}
.mr10 {
  margin-right: 10px;
}

.searchForm .el-form-item--mini.el-form-item,
.searchForm .el-form-item--small.el-form-item {
  margin-bottom: 1px;
}
.study_pic {
  display: block;
  width: 60px;
  height: 60px;
  margin: auto;
}
</style>
